package com.lemon.smartsafety.ui.activity.user;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.OldBaseMode;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.SystemUtil;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.Bind;
import butterknife.OnClick;

public class ResetPassWordEmailActivity extends NewBaseActivity {


    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.metRemarkVerCode)
    MaterialEditText metRemarkVerCode;
    @Bind(R.id.metRemarkPassWord)
    MaterialEditText metRemarkPassWord;
    @Bind(R.id.txt_ok)
    TextView txtOk;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_reset_pass_word_email;
    }

    @Override
    protected void initTitleView() {
        setTitle("重置密码");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    @OnClick({ R.id.txt_ok})
    public void onSubmitClick(View view) {
        switch (view.getId()) {
            case R.id.txt_ok:
                SystemUtil.HideKeyboard(mActivity);
                if (doEmail()) {
                    httpResetPassWord();
                }
                break;
        }

    }

    /**
     * 邮箱验证
     *
     * @return
     */
    private boolean doEmail() {

        if (CommonUtil.strIsEmpty(metRemarkVerCode.getText().toString().trim())) {
            mHUDUtil.showInfoTip("请输入邮箱验证码");
            return false;
        }

        if (CommonUtil.strIsEmpty(metRemarkPassWord.getText().toString().trim())) {
            mHUDUtil.showInfoTip("请输入新密码");
            return false;
        }

        return true;
    }




    /**
     * 重置密码
     */
    private void httpResetPassWord() {

        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("login_name", mSharedPreferencesHelper.getBindLoginName());
        params.put("password", metRemarkPassWord.getText().toString().trim());
        params.put("verify_code", metRemarkVerCode.getText().toString().trim());
        params.put("type", "email");

        showLoading("");

        SharedPreferencesServer mSharedPreferencesServer = new SharedPreferencesServer(this);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(HttpPath.Verify_code_check);

        HttpOkBase.httpBasePost(params, serverUrl, "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    OldBaseMode oldBaseMode = JsonUtils.fromJson(result, OldBaseMode.class);
                    if (!CommonUtil.objIsEmpty(oldBaseMode)) {

                        ToastUtil.show(oldBaseMode.getError().getMsg());
                        if (!oldBaseMode.isIsError()) {
                            finish();
                        } else {
                        }
                    }
                }
            }
        });

    }


}
